﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Data.SqlClient;
using DAL;

namespace PeisongWeb.admin
{
    public partial class dynamicOrderItemAdd : adminPage
    {
        protected int companyId
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["companyId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["companyId"] = value; }
        }
        public int orderid
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["orderId"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["orderId"] = value; }
        }
        public int customerid
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["customerid"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["customerid"] = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["orderId"] != null)
                {
                    orderid = Convert.ToInt32(Request.QueryString["orderid"]);
                    companyId = logic.order.getDynamicById(orderid).CompanyId;
                    customerid = logic.order.getDynamicById(orderid).CustomerId;
                }
                rootCategoryBind();
                subCategoryBind();
                rptBind();

                Panel1.Title = string.Format("#{0}#提前订单添加商品", logic.company.getById(companyId).Company);
            }

        }
        protected void ddl_CategoryRoot_SelectedIndexChanged(object sender, EventArgs e)
        {
            subCategoryBind();
        }
        protected void lbn_reset_Click(object sender, EventArgs e)
        {
        }

        private void rootCategoryBind()
        {
            DataTable dt = logic.buyerRange.rootCategory_buyerRange_Subtraction(companyId);
            ddl_CategoryRoot.DataSource = dt;
            ddl_CategoryRoot.DataBind();
            ddl_CategoryRoot.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }

        private void subCategoryBind()
        {
            //当大类不等于空的时候，就绑定小类
            if (ddl_CategoryRoot.SelectedValue != "")
            {
                DataTable dt = logic.buyerRange.subCategory_buyerRange_Subtraction(companyId, Convert.ToInt32(ddl_CategoryRoot.SelectedValue));
                ddl_CategorySub.DataSource = dt;
                ddl_CategorySub.DataBind();
            }
            else
            {
                ddl_CategorySub.Items.Clear();
                ddl_CategorySub.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
            }
        }

        private void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("buyerId = " + companyId + " and categoryIsShow=1");
            if (!string.IsNullOrEmpty(ddl_CategorySub.SelectedValue))
                where.AppendFormat(" and categoryId={0}", ddl_CategorySub.SelectedValue);
            if (!string.IsNullOrEmpty(txt_ProductName.Text))
                where.AppendFormat(" and [Name] like '%{0}%'", txt_ProductName.Text);
            if (!string.IsNullOrEmpty(txt_ProductNo.Text))
                where.AppendFormat(" and productNo like '{0}'", txt_ProductNo.Text);

            DataSet ds = logic.buyerRange.listWithPrice(where.ToString(), "productId", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }

        // 搜索
        protected void lbn_search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
        protected void btnDelSelected_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(hdSelectedProductIds.Value))
            {
                for (int i = 0; i < rpt_list.Items.Count; i++)      //循环Repeater
                {
                    RepeaterItem row = rpt_list.Items[i];
                    HiddenField hidproId = row.FindControl("hid_productId") as HiddenField;
                    Label lblprice = row.FindControl("lbl_price") as Label;
                    HiddenField lblnature = row.FindControl("hidNature") as HiddenField;
                    TextBox txtQuantity = row.FindControl("txtQuantity") as TextBox;
                    TextBox txtnote = row.FindControl("txtNote") as TextBox;
                    CheckBox cbxUser = row.FindControl("chkSelected") as CheckBox;
                    if (cbxUser.Checked)            //找到选择的商品加入数据库
                    {
                        OrderItemEntity oi = new OrderItemEntity();
                        oi.CustomerId = customerid;
                        oi.Note = txtnote.Text; ;
                        oi.OrderId = orderid;
                        oi.Price = Convert.ToDecimal(lblprice.Text);
                        oi.ProductId = Convert.ToInt32(hidproId.Value);
                        oi.Quantity = Convert.ToDecimal(txtQuantity.Text);
                        oi.InitQuantity = oi.Quantity;
                        oi.BusinessNature = lblnature.Value;
                        PriceEntity pr = logic.price.getByProduct(oi.ProductId, lblnature.Value);
                        oi.SellerId = pr.CompanyId;
                        oi.SellerPrice = pr.SellerPrice;
                        oi.Unit = pr.Unit;
                        logic.orderItem.insertDynamicOrderItem(oi);
                    }
                }

                rptBind();

                Library.Script.ClientMsg("添加成功.");
            }
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("dynamicOrderEdit.aspx?orderid={0}", orderid));
        }
    }
}
